package cn.zyq.ykb.service.msg.impl;

import cn.zyq.ykb.beans.admin.Admin;
import cn.zyq.ykb.beans.msg.Msg;
import cn.zyq.ykb.beans.sys.SysAdmin;
import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.mapper.msg.MsgMapper;
import cn.zyq.ykb.mapper.user.UserMapper;
import cn.zyq.ykb.service.msg.MsgService;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.common.PageParam;
import cn.zyq.ykb.system.enums.MsgTypeEnum;
import cn.zyq.ykb.system.enums.core.ResponseCodeEnum;
import cn.zyq.ykb.system.enums.core.UserPositionEnum;
import cn.zyq.ykb.system.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 消息表
 * 2023-05-10 19:41:40
 */

@Slf4j
@Service
public class MsgServiceImpl extends BaseService implements MsgService {
    @Autowired
    private MsgMapper msgMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public PageInfo<Msg> list(PageParam pageParam, Admin admin) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        List<Msg> dbList = this.msgMapper.list(pageParam.getKv());
        return new PageInfo<>(dbList);
    }

    @Override
    public void add(Msg param, Admin admin) {
        this.setCreateBaseData(param, admin);
        this.msgMapper.insertSelective(param);
    }

    @Override
    public void update(Msg param, Admin admin) {
        Integer id = param.getMsgID();
        this.setUpdateBaseData(param, admin);
        this.msgMapper.updateByPrimaryKeySelective(param);
    }

    @Override
    public void del(String serviceID, Admin admin) {
        Msg dbInfo = this.detail(serviceID);
        Integer id = dbInfo.getMsgID();
        this.deleteLogic(id, admin, this.msgMapper);
    }

    @Override
    public Msg detail(String serviceID, Admin admin) {
        return this.detail(serviceID);
    }

    @Override
    public Msg detail(String serviceID) {
        Msg dbInfo = this.getByServiceID(serviceID, Msg.class, this.msgMapper);
        this.checkLogicExist(dbInfo, "没有获取到消息信息");
        return dbInfo;
    }

    @Override
    public void sendMsg(String content, String serviceID, Integer userID, MsgTypeEnum msgTypeEnum, User loginUser) {
        //新增消息
        Msg addInfo = this.getCreateInstance(Msg.class, loginUser);
        addInfo.setMsgType(msgTypeEnum.getValue());
        addInfo.setTitle(msgTypeEnum.getKey());
        addInfo.setContent(content);
        addInfo.setRelatedServiceID(serviceID);
        addInfo.setUserID(userID);
        this.msgMapper.insertSelective(addInfo);
    }

    @Override
    public void sendMsg(String content, String serviceID, Integer userID, MsgTypeEnum msgTypeEnum, SysAdmin admin) {
        //新增消息
        Msg addInfo = this.getCreateInstance(Msg.class, admin);
        addInfo.setMsgType(msgTypeEnum.getValue());
        addInfo.setTitle(msgTypeEnum.getKey());
        addInfo.setContent(content);
        addInfo.setRelatedServiceID(serviceID);
        addInfo.setUserID(userID);
        this.msgMapper.insertSelective(addInfo);
    }

    @Override
    public PageInfo<Msg> h5List(PageParam pageParam, User loginUser) {
        User user = this.userMapper.selectByPrimaryKey(loginUser.getUserID());
        Integer position = user.getPosition();
        if (!position.equals(UserPositionEnum.TDZ.getValue())){
            throw new BusinessException(ResponseCodeEnum.ERROR,"消息通知仅对团队长开放");
        }
        pageParam.getKv().put("userID", user.getUserID());
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        List<Msg> dbList = this.msgMapper.h5List(pageParam.getKv());
        return new PageInfo<>(dbList);
    }

    @Override
    public List<Map> statistics(User loginUser) {
        return this.msgMapper.statistics(loginUser.getUserID());
    }

    @Override
    public Msg h5Detail(String serviceID, User loginUser) {
        Msg msg = this.detail(serviceID);
        if (msg.getUserID().equals(loginUser.getUserID())){
            throw new BusinessException(ResponseCodeEnum.ERROR, "不能读取他人消息");
        }
        if (msg.getReaded().equals(0)){
            //去更新消息为已读
            msg.setReaded(1);
            this.msgMapper.updateByPrimaryKeySelective(msg);
        }
        return msg;
    }
}
